package com.sc.ktapplication.common.net

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.fastjson.FastJsonConverterFactory
import java.util.concurrent.TimeUnit

class RetrofitManager {

    companion object {
        val instance: RetrofitManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            RetrofitManager()
        }
    }

    private val baseUrl = "http://47.111.67.87:7771/"
    private val defaultTime = 40L //超时时间

    private val okHttpClient = OkHttpClient().newBuilder()
        .connectTimeout(defaultTime, TimeUnit.SECONDS)
        .writeTimeout(defaultTime, TimeUnit.SECONDS)
        .readTimeout(defaultTime, TimeUnit.SECONDS)
        .retryOnConnectionFailure(false)
        .addInterceptor(HttpInterceptor.Builder().build())
        .build()

    private val retrofit = Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl(baseUrl)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(FastJsonConverterFactory.create())
        .build()


    /**
     * 获取对应的Service
     */
    open fun <T> create(service: Class<T>): T {
        return retrofit.create(service)
    }

}